import { Local } from "@/util/storage";
import instance from "@/config/http";
import instance_xc from "@/config_xc/http";

export default {
  data() {
    return {
      // steploading: false,  //登录loading
      isTry: false, // 版本选择弹窗     您当前为体验账号, 可选择以下版本进行体验
      isThirdParty: false, // 第三方登录，显示空白遮罩
      paramsList: ["userName", "pwdVal", "secretVal", "siteUrl"],
    };
  },
  methods: {
    cutUrlParams(variable) {
      var query = window.location.search.substring(1);
      // console.log(query);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        // console.log('pair', pair);
        if (pair[0] == variable) {
          return decodeURIComponent(pair[1]);
        }
      }
      return "";
    },
    // loginSecretValue(params) {
    //   this.$api.
    // },
    loginCustomer(params) {
      params = {
        ...params,
        client_id: "client_pc",
        client_secret: "2cd5b31910183260",
        grant_type: "password",
        route: "LoginCustomer",
      };
      this.$api.publicApi.LoginNonTrace(params).then((res) => {
        if (res.data.status == 200) {
          // 储存来源url
          this.$store.commit("SET_ThirdPartyUrl", params.siteUrl);
          if (this.cutUrlParams("isTry") == "true") this.$store.commit("SET_ISTRIAL", true);
          this.getSetToken(res.data);
        } else {
          // this.$message({ message: res.data.message, type: "warning" });
          this.$alert(res.data.message, "提示", {
            confirmButtonText: "确定",
            callback: (action) => {
              window.opener = null;
              window.open("", "_self");
              window.close();
              // this.$message({
              //   type: 'info',
              //   message: `action: ${action}`
              // });
            },
          });
        }
      });
    },

    getSetToken(resData) {
      //登录后的数据存储
      const {
        data: { userinfo },
        access_token,
      } = resData;
      if (userinfo.isTry && userinfo.isTry == true) {
        // 暂无权限
        this.$store.commit("SET_ISTRIAL", true);
        this.companynumber = userinfo.companynumber;
        this.isTry = true;
        return;
      }
      Local.clear(); // 清除缓存
      let basicInformation = {
        UserId: userinfo.userid, // 用户ID
        CompanyId: userinfo.companyid, // 企业ID
        Serial: userinfo.companynumber, // 企业号
        // realname: userinfo.realname, // 真实姓名
        // username: userinfo.username, //用户名
        // RoleId: userinfo.roleid, // 角色ID
        // isadmin: userinfo.isadmin, // 是否超级管理员
        // departmentid: userinfo.departmentid, // 部门id
        PSIID: userinfo.psiId,
        // appKey: userinfo.appKey,
        // appSecret: userinfo.appSecret,
        // credentialId: userinfo.credentialId,
        // profile: userinfo.profile, // 头像
        // position: userinfo.position, //职位
        ...userinfo,
        // isApprove: userinfo.isApprove, //是否开通审批云
        // isPurchase: userinfo.isPurchase, //是否开通进销存
      };
      sessionStorage.setItem("smallType", basicInformation.smallType);
      this.$store.dispatch("GetUserInfo", basicInformation);
      this.$bus.$emit("GetisWork", userinfo.isOpenWatermark);
      console.log(userinfo, "userinfouserinfouserinfouserinfouserinfouserinfouserinfouserinfo");

      instance.defaults.headers["Authorization"] = "Bearer " + access_token;
      instance_xc.defaults.headers["Authorization"] = "Bearer " + access_token;
      this.$store.dispatch("GetToken", "Bearer " + access_token); // 保存请求头
      this.$store.dispatch("getIsLogin", true);
      // this.$store.dispatch("setExpireData", { banner: true, window: true });
      this.checkExpire();
      this.$emit("getLogin", resData);
    },
    checkExpire() {
      this.$api.publicApi.CheckExpire().then((res) => {
        console.log(res, "checkExpire");
        if (res.data.data.isWillExpire) {
          const newData = {
            window: !res.data.data?.isRead,
            banner: res.data.data?.isWillExpire,
            expire: res.data.data?.isWillExpire,
            endDate: res.data.data?.serviceEndDate,
          };
          this.$store.dispatch("setExpireData", newData);
        }
      });
    },
  },
};
